package com.cpo.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.cpo.dao.LoginDao;
import com.cpo.dao.UserRegistrationDao;
import com.cpo.service.LoginServiceImpl;
import com.cpo.validators.LoginValidator;
import com.cpo.domain.Login;
import com.cpo.interfaces.LoginService;

@Controller
@SessionAttributes({"login"})
@Scope(value="session")
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	@Autowired
	private LoginValidator loginValidator;
	
	@RequestMapping(value="/loginUser", method=RequestMethod.POST)	
	public ModelAndView create(@ModelAttribute("login")Login login, BindingResult result, SessionStatus status)
	{
		if(login.getUserName()==null || login.getPassword()==null){
			ModelAndView mav1 =  new ModelAndView();
			String page ="redirect:loginFailed.do";
			mav1 = new ModelAndView(page);
			return mav1;
		}
		String page ="loginFailed";
		ModelAndView mav =  new ModelAndView(page);
		loginValidator.validate(login, result);
		if (result.hasErrors()){
			page = "login";
			mav = new ModelAndView(page);
/*			List<Login> userDetails = loginService.getUserDetails(login);
			mav.getModelMap().put("USER_DETAILS", userDetails);	*/
			return mav;
		}
		
		if(!loginService.isUserPresent(login) ){
			boolean isValid = loginService.validateUser(login);
			if(isValid && loginService.isLoginLocked(login)){
				boolean time = loginService.checkTime(login);
				if(time){
					loginService.updateLock(login);
				}
				else{
					loginService.updateAttempt(login);
				}
			}
			if(isValid && !loginService.isLoginLocked(login)){
				loginService.updateAttempt(login);
				page = "home";
				if (!result.hasErrors()) 
				{
					mav = new ModelAndView(page);
				//return mav;
				}
		}
			else if(isValid && loginService.isLoginLocked(login)){
				page = "login";
				if (!result.hasErrors()) 
				{		
					//page="login";
					List<Login> userDetails = loginService.getUserDetails(login);
					mav = new ModelAndView(page);
					mav.getModelMap().put("USER_DETAILS", userDetails);
					return mav;
				}
			}
			else if(isValid && !loginService.isLoginLocked(login)){
				
				page = "login";
				if (!result.hasErrors()) 
				{		
					//page="login";
					List<Login> userDetails = loginService.getUserDetails(login);
					mav = new ModelAndView(page);
					mav.getModelMap().put("USER_DETAILS", userDetails);
					//return mav;
				}
			}
			else if(!isValid && !loginService.isLoginLocked(login)){
				page = "login";
				loginService.updateAttempt(login);
				int i = loginService.checkNoOfAttempts(login);
				if(i==0){
				loginService.lockUserAccount(login);
				status.setComplete();
				List<Login> userDetails1 = loginService.getUserDetails(login);
				mav = new ModelAndView(page);
				mav.getModelMap().put("USER_DETAILS", userDetails1);
				return mav;
				}
				else{
					
					List<Login> userDetails = loginService.getUserDetails(login);
					mav = new ModelAndView(page);
					mav.getModelMap().put("USER_DETAILS", userDetails);
					//status.setComplete();
					return mav;
				}
			}
			else if(!isValid && loginService.isLoginLocked(login)){
				page = "login";
				List<Login> userDetails1 = loginService.getUserDetails(login);
				mav = new ModelAndView(page);
				mav.getModelMap().put("USER_DETAILS", userDetails1);
				return mav;
			}
			
			
			else
			page = "loginFailed";
			mav = new ModelAndView(page);
		
		
		}
		
		return mav;
	}
	@RequestMapping(value="/loginFailed", method=RequestMethod.GET)
	public ModelAndView getLoginFailed()
	{
		ModelAndView mav = new ModelAndView("loginFailed");	
		return mav;
	}
	
	
	@RequestMapping(value="/loginUser", method=RequestMethod.GET)
	public ModelAndView getLoginForm()
	{
		ModelAndView mav = new ModelAndView("login");
		Login login = new Login();
		mav.getModelMap().put("login", login);
		return mav;
	}
	
	@RequestMapping("/home")
	public ModelAndView getHome()
	{
		ModelAndView mav = new ModelAndView("home");

		return mav;
	}
}
	

